<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
class Csnapshot extends controller {
	const ISWG   = 0;
	const LIMIT  = 12;
	const URL    = 'http://52.53.241.18';
	const BASEURL= '/admin/live/monitor/snapshot/home.html';
	const PIC    = 'http://52.53.241.18/121095_55403_001/121095_55403_001_20170412075253.jpg';

	public function main() {
		$view   = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$url  = self::BASEURL;
		$view->assign_by_ref('url', $url);
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/monitor/snapshot.ext.js');
	}
	
	public function home() {
		$view   = C\view::inst('html');
		Cadmin::chkaccess($view);
		$where = array('olstatus'=>1);
		$key   = trim(input::get_post('key'));
		if ($key) {
			$where[] = array('(');
			$where[] = array('author', '%'.$key.'%', \classlib\orm::OP_LIKE);
			$where[] = array('title', '%'.$key.'%', \classlib\orm::OP_LIKE, \classlib\orm::OP_OR);
			$where[] = array(')');
		}
		$status= input::get_post('status');
		if (is_numeric($status)) {
			$where['isdoubt'] = $status;
		}
		$orm    = C\model::load('pro.pl.studio');
		$total  = $orm->get_total($where);
		$data   = array('total'=>0, 'list'=>array());
		if ($total) {
			$list = $orm->get_list(0, self::LIMIT, $where, 'userid,title,studiono,author,isdoubt as status');
			
			$sorm   = C\model::load('log.log.screen');
			$aorm   = C\model::load('pro.pl.anchor');
			$rorm   = C\model::load('pro.inc.star');
			$config = C\config::load('config', false);
			$time   = C\config::$nowtime - 90;
			foreach ($list as $item) {
				$where  = array('userid'=>$item['userid'], 'iswg'=>self::ISWG, 'isnew'=>1, array('stime', $time, \classlib\orm::OP_GT));
				$screen = $sorm->get_item($where);
				$anchor = $aorm->get_one($item['userid']);
				$star   = $rorm->get_one($anchor['starlv']);
				$data['list'][] = array(
						'id'    => $screen['id'],
						'userid'=> $item['userid'],
						'thumb' => !empty($screen) ? self::URL.$screen['thumb'] : self::PIC,
						'link'  => $config['website'].C\router::get_url('live', $item['studiono']),
						'author'=> $item['author'],
						'status'=> $item['status'],
						'title' => mb_substr($item['title'], 0, 12, 'UTF-8'),
						'starlv'=> $star['name']
				);
			}
			$data['total'] = $total;
		}
		$totalPages = ceil($data['total']/self::LIMIT);
		$dourl = C\router::get_url('admin-live-monitor-snapshot', 'dolist');
		$doset = C\router::get_url('admin-live-monitor-snapshot', 'doset');
		$baseurl = self::BASEURL;
		//判断设为怀疑的主播是否下线
		$where = array('olstatus'=>0, 'isdoubt'=>1);
		$studio= C\model::load('pro.pl.studio')->get_column(0, -1, $where, 'author');
		if (!empty($studio)) {
			$studio = implode(', ', $studio);
		}
		$view->assign_by_ref('key', $key);
		$view->assign_by_ref('status', $status);
		$view->assign_by_ref('dourl', $dourl);
		$view->assign_by_ref('doset', $doset);
		$view->assign_by_ref('baseurl', $baseurl);
		$view->assign_by_ref('list', $data['list']);
		$view->assign_by_ref('total', $data['total']);
		$view->assign_by_ref('totalPages', $totalPages);
		$view->assign_by_ref('studio', $studio);
		$view->display('live/monitor/img.html');
	}
	
	public function dolist() {
		$view  = C\view::inst('html');
		Cadmin::chkaccess($view);
		$page  = (int)input::get_post('page');
		if ($page < 1) {
			$page= 1;
		}
		$limit = self::LIMIT;
		$start = ($page - 1) * $limit;
		$where = array('olstatus'=>1);
		$key   = trim(input::get_post('key'));
		if ($key) {
			$where[] = array('(');
			$where[] = array('author', '%'.$key.'%', \classlib\orm::OP_LIKE);
			$where[] = array('title', '%'.$key.'%', \classlib\orm::OP_LIKE, \classlib\orm::OP_OR);
			$where[] = array(')');
		}
		$status= input::get_post('status');
		if (is_numeric($status)) {
			$where['isdoubt'] = $status;
		}
		$orm    = C\model::load('pro.pl.studio');
		$total  = $orm->get_total($where);
		$data   = array('total'=>0, 'list'=>array());
		if ($total) {
			$list   = $orm->get_list($start, $limit, $where, 'userid,title,studiono,author,isdoubt as status');
			$sorm   = C\model::load('log.log.screen');
			$aorm   = C\model::load('pro.pl.anchor');
			$rorm   = C\model::load('pro.inc.star');
			$config = C\config::load('config', false);
			$time   = C\config::$nowtime - 90;
			foreach ($list as $item) {
				$anchor = $aorm->get_one($item['userid']);
				$star   = $rorm->get_one($anchor['starlv']);
				$where  = array('userid'=>$item['userid'], 'iswg'=>self::ISWG, 'isnew'=>1, array('stime', $time, \classlib\orm::OP_GT));
				$screen = $sorm->get_item($where);
				$data['list'][] = array(
						'id'    => $screen['id'],
						'userid'=> $item['userid'],
						'thumb' => !empty($screen) ? self::URL.$screen['thumb'] : self::PIC,
						'link'  => $config['website'].C\router::get_url('live', $item['studiono']),
						'author'=> $item['author'],
						'status'=> $item['status'],
						'title' => mb_substr($item['title'], 0, 12, 'UTF-8'),
						'starlv'=> $star['name']
				);
			}
			$data['total'] = $record['total'];
			$view->assign_by_ref('data', $data);
			$view->display('live/monitor/module.html');
		}
	}
	
	public function doset() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids  = input::get_post('ids');
		$ids  = explode(',', $ids);
		if(!is_array($ids)) {
			$ids = array($ids);
		}
		$ids   = array_filter($ids, 'is_numeric');
		if(empty($ids)) {
			$view->assign_error('1', '请选择要处理的记录');
		}
		$status= (int)input::get_post('status');
		$table = C\model::load('pro.pl.studio')->get_table();
		$orm   = C\model::load('pro.pl.studio')->get_orm();
		foreach ($ids as $id) {
			$field = array('isdoubt'=>$status);
			$orm->clear()->table($table)->where('userid', $id)->update($field);
		}
		C\model::load('pro.pl.studio')->save_cache_ver();
		$view->display();
	}
	
	public function log() {
		$view  = C\view::inst('html');
		Cadmin::chkaccess($view);
		$where = array('a.iswg'=>self::ISWG);
		$userid= (int)input::get_post('userid');
		if ($userid) {
			$where['a.userid'] = $userid;
		}
		$fields= 'a.id,a.userid,a.thumb,b.isdoubt as status,b.title,b.author,b.studiono';
		$data  = C\model::load('log.log.screen')
			->sign_list(0, self::LIMIT, $where, $fields, 'a.id', \classlib\orm::DESC);
		if ($data['total'] > 0) {
			$config = C\config::load('config', false);
			foreach ($data['list'] as &$item) {
				$item['thumb'] = self::URL.$item['thumb'];
				$item['link']  = $config['website'].C\router::get_url('live', $item['studiono']);
				$item['title'] =  mb_substr($item['title'], 0, 13, 'UTF-8');
			}
		}
		$totalPages = ceil($data['total']/self::LIMIT);
		$dourl = C\router::get_url('admin-live-monitor-snapshot', 'loglist');
	
		$view->assign_by_ref('userid', $userid);
		$view->assign_by_ref('dourl', $dourl);
		$view->assign_by_ref('list', $data['list']);
		$view->assign_by_ref('total', $data['total']);
		$view->assign_by_ref('totalPages', $totalPages);
		$view->display('live/monitor/log.html');
	}
	
	public function loglist() {
		$view  = C\view::inst('html');
		Cadmin::chkaccess($view);
		$page  = (int)input::get_post('page');
		if ($page < 1) {
			$page= 1;
		}
		$limit = self::LIMIT;
		$start = ($page - 1) * $limit;
		$where = array('a.iswg'=>self::ISWG);
		$userid= (int)input::get_post('userid');
		if ($userid) {
			$where['a.userid'] = $userid;
		}
		$field = 'a.id,a.userid,a.thumb,b.isdoubt as status,b.title,b.author,b.studiono';
		$data  = C\model::load('log.log.screen')
			->sign_list($start, $limit, $where, $field, 'a.id', \classlib\orm::DESC);
		if ($data['total'] == 0) {
			exit;
		}
		$config = C\config::load('config', false);
		foreach ($data['list'] as &$item) {
			$item['thumb'] = self::URL.$item['thumb'];
			$item['link']  = $config['website'].C\router::get_url('live', $item['studiono']);
			$item['title'] =  mb_substr($item['title'], 0, 13, 'UTF-8');
		}
		$view->assign_by_ref('data', $data);
		$view->display('live/monitor/module.html');
	}
	
}